﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;


public partial class _Default : AlbumPage
{
    private void Page_Load(object sender, EventArgs e)
    {       
            gv.DataSource = GetViewModels();
            gv.DataBind();       
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        gv.DataBind();
    }

    protected void lstPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        gv.PageSize = int.Parse(lstPageSize.SelectedItem.Value);
        gv.DataBind();
    }

    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewModelHelper modelHelper = new ViewModelHelper();
        gv.DataSource = modelHelper.SortArray(GetViewModels(), e.SortExpression, GetSortDirection(e.SortExpression));
        gv.DataBind();
    }

    private string GetSortDirection(string column)
    {
        string sortDirection = "ASC";
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {           
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtBoxSearch.Text != "")
        {
            ViewModelHelper modelHelper = new ViewModelHelper();
            btnBack.Visible = true;
            gv.DataSource = modelHelper.GetFiltratedModels(GetViewModels(), txtBoxSearch.Text);
            gv.DataBind(); 
        }
        else
        {
            btnBack.Visible = false;
            gv.DataSource = GetViewModels();
            gv.DataBind(); 
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        btnBack.Visible = false;
        gv.DataSource = GetViewModels();
        gv.DataBind(); 
    }
}
